<template>
	 <mdp-table v-if="crudApis.list!=null" :subOpType="currOpType" :multiple="multiple" operWidth="0" :params="{id:modelValue}"  ref="tableFieldListRef" :columnCfgs="columnCfgs" :crudApis="crudApis" @select="(v)=>$emit('select',v)"/>
</template> 
<script> 
import {queryDataSetById} from '@/api/mdp/dm/dataSet'



export default {
	name: 'DataSetFieldList', 
	props: {
		modelValue: {
			type:[String,Array,Object],
			default:function(){
				return null
			}
		},
		subOpType:{
			type:String,
			default: 'mng'
		},
		multiple:{
			type: Boolean,
			default: false
		}
	},
	data(){
		return {  
			columnCfgs:[
				
			  {label:'字段名称',property:'columnName'},
			  {label:'字段描述',property:'remarks'},
			  {label:'字段类型',property:'dataTypeName'},
			  {label:'字段长度',property:'columnSize'},
				{label:'表名',property:'tableName'}, 
			],
			crudApis:{
				list:null,
			},  
		}
	},
	computed: {
		currOpType(){
			return this.subOpType
		},
		value:{
			set(val){
				this.$emit('update:modelValue',val)
			},
			get(){
				return this.modelValue
			}
		},
		
	}, 
	methods:{ 
		idTitleLinksMap(idTitleLinks){
			var map=new Map();
			if(!idTitleLinks){
				return map;
			}
			var links=JSON.parse(idTitleLinks)
			links.forEach(k=>{
				map.set(k.id,k.title)
			})
			return map
		},
		listDataSetField(params){
			var promise=new Promise((resolve, reject)=>{
				queryDataSetById(params).then(res=>{
					var res2={data:res.fields,total:res.fields.length,tips:res.tips}
					var map=this.idTitleLinksMap(res.data.idTitleLinks)
					res2.data=res.fields.map(k=>{
						var field={...k}
						field.remarks=map.get(this.$mdp.toCamel(k.columnName))||field.remarks
						return field
					})
					resolve(res2)
				})
			}) 
			return promise
		},
		doSearch(){  
			if(!this.value){
				return;
			}
			this.$refs['tableFieldListRef']?.searchTableDatas();
		}
	},
	mounted() {  
		this.crudApis.list=this.listDataSetField 
	}
}
</script>
